package com.windoent.yugioh.ui.activity.splash;

import android.app.Activity;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.RelativeLayout;

import com.baidu.mobstat.IIgnoreAutoTrace;
import com.mob.MobSDK;
import com.windoent.yugioh.AppManager;
import com.windoent.yugioh.MainActivity;
import com.windoent.yugioh.R;
import com.windoent.yugioh.base.BaseActivity;
import com.windoent.yugioh.constants.Constants;
import com.windoent.yugioh.ui.activity.registlogin.LoginActivity;
import com.windoent.yugioh.ui.activity.registlogin.PrivacyActivity;
import com.windoent.yugioh.ui.activity.registlogin.RegistPrivacyActivity;
import com.windoent.yugioh.utils.BaseMethod;
import com.windoent.yugioh.utils.SharedPreUtil;
import com.windoent.yugioh.widget.dialog.PrivacyDialog;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.functions.Consumer;

/**
 * author : robert
 * date   : 2021/5/6 13:20
 * desc   :启动页（百度统计-禁止页面自动统计）
 */
public class SplashActivity extends BaseActivity implements IIgnoreAutoTrace {
    private int delayTimeMills = 400;
    private RelativeLayout relativeLayout;

    @Override
    public int getLayout() {
        return R.layout.activity_splash;
    }

    @Override
    public void initData() {
        super.initData();
        immersiveStatusBar();
        // 防止第三方跳转时出现双实例
        Activity aty = AppManager.getActivity(MainActivity.class);
        if (aty != null && !aty.isFinishing()) {
            finish();
        }
        relativeLayout = (RelativeLayout) findViewById(R.id.rl_splash);
        boolean showPrivacy = SharedPreUtil.getBoolean(SplashActivity.this, Constants.SHOWPRIVACY, false);
        if (!showPrivacy) {
            showPrivacyDialog();
        } else {
            animation();
        }
    }

    private void animation() {
        if (relativeLayout != null) {
            animate(relativeLayout);
        } else {
            Observable.timer(delayTimeMills, TimeUnit.MILLISECONDS).subscribe(new Consumer() {
                @Override
                public void accept(Object o) throws Exception {
                    toMianOrLogIn();
                }
            });
        }
    }

    //显示隐私dialog
    private void showPrivacyDialog() {
        PrivacyDialog dialog = new PrivacyDialog(mContext);
        dialog.setMessage(getString(R.string.string_privacy_text));
        dialog.setTitle(getString(R.string.string_privacy));
        dialog.setYesOnclickListener(getString(R.string.string_agreed), new PrivacyDialog.onYesOnclickListener() {
            @Override
            public void onYesClick() {
                SharedPreUtil.put(SplashActivity.this, Constants.SHOWPRIVACY, true);
                MobSDK.submitPolicyGrantResult(true, null);
                animation();
            }
        });
        dialog.setNoOnclickListener(getString(R.string.string_disagreed), new PrivacyDialog.onNoOnclickListener() {
            @Override
            public void onNoClick() {
                //退出
                finish();
                System.exit(0);
            }
        });
        dialog.setRegistOnclickListener(new PrivacyDialog.onRegistOnclickListener() {
            @Override
            public void onClick() {
                //用户注册协议
                startActivity(new Intent(mContext, RegistPrivacyActivity.class));
            }
        });
        dialog.setPrivacyOnclickListener(new PrivacyDialog.onPrivacyOnclickListener() {
            @Override
            public void onClick() {
                //隐私协议
                startActivity(new Intent(mContext, PrivacyActivity.class));
            }
        });
        dialog.show();
    }

    private void animate(RelativeLayout relativeLayout) {
        // 渐变展示启动屏
        AlphaAnimation aa = new AlphaAnimation(0.5f, 1.0f);
        aa.setDuration(delayTimeMills);
        relativeLayout.startAnimation(aa);
        aa.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationEnd(Animation arg0) {
                toMianOrLogIn();
                //检查登录
                //checkLogIn();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationStart(Animation animation) {
            }
        });
    }


    private void toMianOrLogIn() {
        String userToken = getUserToken();
        if (BaseMethod.notNull(userToken) && userToken.length() > 0) {
            startActivity(new Intent(mContext, MainActivity.class));
        } else {
            startActivity(new Intent(mContext, LoginActivity.class));
        }
        finish();
    }


    //    Android禁用返回键
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            //do something.
            return true;
        } else {
            return super.dispatchKeyEvent(event);
        }
    }

}
